package com.hangwei.paperless.view.window;

import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hangwei.paperless.MainSeeActivity;
import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.BasicInfoBean;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.view.BaseView;
import com.twelve.all.net.API;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.Tools;

/**
 * Created by 张龙臣 on 2020/11/24.
 */

public class BasicInfoSee extends BaseView<MainSeeActivity, BasicInfoBean> {
    public BasicInfoSee(MainSeeActivity activity) {
        super(activity);
    }

    @Override
    protected View initView() {
        View inflate = LayoutInflater.from(mActivity).inflate(R.layout.page_see_basic_info, null);
        initData();
        return inflate;
    }

    @Override
    public void bindData(BasicInfoBean bean) {
        mItemView.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });
        TextView tvPN = (TextView) mItemView.findViewById(R.id.tvPN);
        TextView tvSN = (TextView) mItemView.findViewById(R.id.tvSN);
        TextView tvRevNum = (TextView) mItemView.findViewById(R.id.tvRevNum);
        TextView tvProduct = (TextView) mItemView.findViewById(R.id.tvProduct);
        TextView tvWoNo = (TextView) mItemView.findViewById(R.id.tvWoNo);
        TextView tvNum = (TextView) mItemView.findViewById(R.id.tvNum);
        TextView tvDes = (TextView) mItemView.findViewById(R.id.tvDes);
        TextView tvRevVersion = (TextView) mItemView.findViewById(R.id.tvRevVersion);
        TextView tvPreparedByDate = (TextView) mItemView.findViewById(R.id.tvPreparedByDate);
        TextView tvCheckedByData = (TextView) mItemView.findViewById(R.id.tvCheckedByData);
        TextView tvApprovedByDate = (TextView) mItemView.findViewById(R.id.tvApprovedByDate);
        SimpleDraweeView sdvPreparedBySign = mItemView.findViewById(R.id.sdvPreparedBySign);
        SimpleDraweeView sdvCheckedBySign = mItemView.findViewById(R.id.sdvCheckedBySign);
        SimpleDraweeView sdvApprovedBySign = mItemView.findViewById(R.id.sdvApprovedBySign);
        ImageView ivApproved = mItemView.findViewById(R.id.ivApproved);
        ImageView ivPrepared = mItemView.findViewById(R.id.ivPrepared);
        ImageView ivChecked = mItemView.findViewById(R.id.ivChecked);

        tvPN.setText(Tools.checkedString(bean.partno));
        tvSN.setText(Tools.checkedString(bean.sn));
        tvProduct.setText(Tools.checkedString(bean.product));
        tvWoNo.setText(Tools.checkedString(bean.woNo));
        tvNum.setText(Tools.checkedString(bean.number));
        tvDes.setText(Tools.checkedString(bean.description));
        tvRevVersion.setText(Tools.checkedString(bean.revVersion));
        if (bean.manualList != null && bean.manualList.size() > 0) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < bean.manualList.size(); i++) {
                BasicInfoBean.ManualListBean listBean = bean.manualList.get(i);
                if (listBean.disabled)
                    builder.append("<font color='#ff0000' >").append(listBean.revNo).append("</font>");
                else
                    builder.append("<font color='#333333' >").append(listBean.revNo).append("</font>");
                if (i != bean.manualList.size() - 1)
                    builder.append("<font color='#ff0000' >,&nbsp</font>");
            }
            tvRevNum.setText(Html.fromHtml(builder.toString()));
        } else
            tvRevNum.setText("");
        if (bean.preparedById == null) {
            sdvPreparedBySign.setVisibility(View.GONE);
            tvPreparedByDate.setVisibility(View.GONE);
            ivPrepared.setVisibility(View.VISIBLE);
            ivPrepared.setOnClickListener(this);
        } else {
            sdvPreparedBySign.setVisibility(View.VISIBLE);
            tvPreparedByDate.setVisibility(View.VISIBLE);
            ivPrepared.setVisibility(View.GONE);
            sdvPreparedBySign.setImageURI(API.BASE_URL + bean.preparedBySign);
            tvPreparedByDate.setText(bean.preparedDate);
        }
        if (bean.checkedById == null) {
            sdvCheckedBySign.setVisibility(View.GONE);
            tvCheckedByData.setVisibility(View.GONE);
            ivChecked.setVisibility(View.VISIBLE);
            ivChecked.setOnClickListener(this);
        } else {
            sdvCheckedBySign.setVisibility(View.VISIBLE);
            tvCheckedByData.setVisibility(View.VISIBLE);
            ivChecked.setVisibility(View.GONE);
            sdvCheckedBySign.setImageURI(API.BASE_URL + bean.checkedBySign);
            tvCheckedByData.setText(bean.checkedDate);
        }
        if (bean.approvedById == null) {
            sdvApprovedBySign.setVisibility(View.GONE);
            tvApprovedByDate.setVisibility(View.GONE);
            ivApproved.setVisibility(View.VISIBLE);
            ivApproved.setOnClickListener(this);
        } else {
            sdvApprovedBySign.setVisibility(View.VISIBLE);
            tvApprovedByDate.setVisibility(View.VISIBLE);
            ivApproved.setVisibility(View.GONE);
            sdvApprovedBySign.setImageURI(API.BASE_URL + bean.approvedBySign);
            tvApprovedByDate.setText(bean.approvedDate);
        }

    }

    private void initData() {
        final LoadingDialog dialog = new LoadingDialog(mActivity);
        dialog.show();
        String userId = SPUtil.getString(SPKey.Account.USER_ID);
        StaticPresenter.getRoutingSheetInfo(userId, mActivity.getBillId(), new CallBack<BasicInfoBean>() {
            @Override
            public void onCompleted(BasicInfoBean basicInfoBean) {
                dialog.dismiss();
                bindData(basicInfoBean);
            }

            @Override
            public void onError() {
                dialog.dismiss();
            }
        });
    }

}
